﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Windows.Markup;


namespace SDF.Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class SmartDocuments : Window
    {
        public SmartDocuments()
        {
            this.DataContext = new SmartDocumentsViewModel();
            InitializeComponent();
        }
    }

    public class SmartDocumentsViewModel : INotifyPropertyChanged
    {
        #region Constructor

        public SmartDocumentsViewModel()
        {
            RefreshDocuments();
        }

        #endregion

        #region Constants

        protected const string FOLDER = @"C:\Userdata\Sandbox\SDF\SDF.Test\SampleDocuments\SmartDocuments";

        #endregion

        #region Properties

        public SmartDocumentPaginator SmartPaginator { get; private set; }

        public Dictionary<string, SmartDocument> SampleDocuments { get; private set; }

        public string SelectedDocumentName
        {
            get { return _SelectedDocumentName; }
            set
            {
                if (_SelectedDocumentName != value)
                {
                    _SelectedDocumentName = value;
                    RaisePropertyChanged("SelectedDocumentName");
                    UpdateSelectedDocument();
                }
            }
        }
        private string _SelectedDocumentName;

        public SmartDocument SelectedDocument
        {
            get { return _SelectedDocument; }
            private set
            {
                if (_SelectedDocument != value)
                {
                    _SelectedDocument = value;
                    RaisePropertyChanged("SelectedDocument");
                    Repaginate();
                }
            }
        }
        private SmartDocument _SelectedDocument;

        public IDocumentPaginatorSource PaginatedDocument
        {
            get { return _PaginatedDocument; }
            private set
            {
                if (_PaginatedDocument != value)
                {
                    _PaginatedDocument = value;
                    RaisePropertyChanged("PaginatedDocument");
                }
            }
        }
        private IDocumentPaginatorSource _PaginatedDocument;

        #endregion

        #region Methods

        public void RefreshDocuments()
        {
            SampleDocuments = new Dictionary<string, SmartDocument>();
            var folder = new DirectoryInfo(FOLDER);
            if (!folder.Exists)
            {
                MessageBox.Show(String.Format("The folder '{0}' could not be found.", FOLDER));
            }
            else
            {
                foreach (var file in folder.EnumerateFiles())
                {
                    SampleDocuments.Add(file.Name, null);
                }

                RaisePropertyChanged("SampleDocument");
                SelectedDocumentName = SampleDocuments.FirstOrDefault().Key;
            }
        }

        private void UpdateSelectedDocument()
        {
            var document = SampleDocuments[SelectedDocumentName];
            if (document == null)
            {
                document = LoadDocument(SelectedDocumentName);
            }
            SelectedDocument = document;
        }

        private SmartDocument LoadDocument(string SelectedDocumentName)
        {
            string path = String.Format("{0}\\{1}", FOLDER, SelectedDocumentName);
            var stream = File.OpenRead(path);
            var reader = XmlReader.Create(stream);
            var document = XamlReader.Load(reader) as SmartDocument;
            return document;
        }

        private void Repaginate()
        {
            var MicrosoftPaginator = ((IDocumentPaginatorSource)new FlowDocument()).DocumentPaginator;

            SmartPaginator = new SmartDocumentPaginator(SelectedDocument);
            SmartPaginator.PageSize = MicrosoftPaginator.PageSize;
            var smartWrapper = new XpsDocumentWrapper(SmartPaginator);
            PaginatedDocument = smartWrapper.FixedDocumentSequence;
        }


        #endregion

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _PropertyChanged += value; }
            remove { _PropertyChanged += value; }
        }
        private PropertyChangedEventHandler _PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (_PropertyChanged != null)
                _PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

}
